<?php

namespace App\Admin\Actions\NetworkGoods;

use App\Models\yb\NetworkGoodsReasonModel;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;



class MyGoodsBatchOffShelf extends BatchAction
{
    protected $selector = '.my-goods-batch-off-shelf';

    public function handle(Collection $collection, Request $request)
    {
        $content = $request->get('content');

        try {
            foreach ($collection as $model) {
                $model->status = 0;
                $model->save();
                NetworkGoodsReasonModel::insert([
                    'network_id' => $model->network_id,
                    'goods_id' => $model->goods_id,
                    'network_goods_id' => $model->id,
                    'created_at' => date('Y-m-d H:i:s',time()),
                    'content' => $content,
                ]);
            }
            return $this->response()->success('操作成功')->refresh();

        } catch (Exception $e) {
            return $this->response()->error('产生错误：'.$e->getMessage());
        }

    }

    public function form()
    {
        $this->textarea('content','下架原因')->required();

    }

    public function html()
    {
        return "<a class='my-goods-batch-off-shelf btn btn-sm btn-warning' style='margin-left: 5px;'><i class='fa fa-info-circle'></i>&nbsp;批量下架</a>";
    }

}